package com.x.rocketmq;

import com.x.rocketmq.RmqConfig.RmqTopic;
import java.lang.reflect.Field;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务层 RMQ 生产者服务
 * @author xjk
 * @date 2025-07-03
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BusinessRmqProducerService {

  private final RmqConfig rmqConfig;
  @Autowired
  RmqProducerService producerService;


  private String getTopic(String methodName){
    RmqTopic topic = rmqConfig.getTopic();
    try {
      Field f = topic.getClass().getDeclaredField(methodName);
      f.setAccessible(true);
      Object o = f.get(topic);
      return String.valueOf(o);
    }catch (NoSuchFieldException | IllegalAccessException e){
      log.error("获取topic失败",e);
      throw new RuntimeException("获取topic失败");
    }
  }

  public <T> void sendByMethodName(String methodName, T body) {
    producerService.sendMsgToMq(this.getTopic(methodName), methodName, body);
  }

}
